import Router from 'koa-router';
import proxyService from '../../common/proxyService';
import {
  orderManageList, // 订单列表
  orderManageDetail, // 订单列表
} from '../../api/agent';

const router = new Router();

const companyName = v => {
  if (v) {
    const len = v.length;
    if (len < 6) {
      return v.slice(0, 2) + '******';
    } else {
      return v.slice(0, 2) + '******' + v.slice(len - 2);
    }
  } else {
    return '';
  }
};

router.post('/getOrderListByPageForM', async ctx => {
  const response = await proxyService.postProxy(ctx, orderManageList);
  if (response.code === 1) {
    const list = response.data.records || [];
    list.forEach(v => {
      if (v.childAgent === 1) {
        v.corpName = companyName(v.corpName);
      }
    });
  }
  return (ctx.body = response);
});

router.post('/getDetailForM', async ctx => {
  const response = await proxyService.postProxy(ctx, orderManageDetail);
  if (response.code === 1) {
    const dat = response.data;
    if (dat.childAgent === 1) {
      dat.corpName = companyName(dat.corpName);
    }
  }
  return (ctx.body = response);
});

export default router;
